package com.nas.app;

import java.text.ParseException;
import java.util.Date;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import com.nas.app.process.ThreadControl;
import com.nas.app.process.ThreadUniqueId;
import com.nas.app.task.Parse;
import com.nas.app.task.Task;
import com.nas.app.task.command.Command;
import com.nas.app.task.command.Interpreter;
import com.nas.app.Message;

public class ShellCli implements Runnable {

    private static String CHATCLI = "ChatCli";

    private BlockingQueue<Message> inMessageQueue;

    private Dictionary<Long, ThreadControl> threads;

    private Parse parse;

    public ShellCli() {
        parse = new Parse();
        inMessageQueue = new PriorityBlockingQueue<Message>();
        threads = new Hashtable<Long, ThreadControl>();

        ChatCli chatCli = new ChatCli(inMessageQueue);
        chatCli.setAppThreadName(CHATCLI);
        chatCli.setAppThreadId(ThreadUniqueId.get());
        Thread chatCliThread = new Thread(chatCli);
        ThreadControl chatCliThreadControl = new ThreadControl();
        chatCliThreadControl.setThreadControllable(chatCli);
        chatCliThreadControl.setThread(chatCliThread);
        chatCliThreadControl.setInQueue(null);
        chatCliThreadControl.setOutQueue(chatCli.getInMessageQueue());

        threads.put(chatCli.getAppThreadId(), chatCliThreadControl);
    }

    @Override
    public void run() {
        for (Enumeration<ThreadControl> elements = threads.elements(); elements.hasMoreElements();) {
            elements.nextElement().getThread().start();
        }
        try {
            while (true) {
                loadMessage(inMessageQueue.take());
            }
        } catch (InterruptedException ex) {
            System.out.println("ShellCli-Exception: " + ex);
        }
    }

    private void loadMessage(Message message) {
        Message responseMessage = new Message();
        responseMessage.setType(Message.Type.RAW);
        responseMessage.setFromThreadId(null);
        responseMessage.setThreadId(null);
        responseMessage.setToThreadId(message.getThreadId());

        ThreadControl threadControl = threads.get(message.getThreadId());

        if (CHATCLI.equals(threadControl.getThreadControllable().getAppThreadName())) {
            if (message.getMessage().trim().equals("exit")) {
                System.out.println("Cerrando");
                System.exit(0);
            }
            try {
                Task task = parse.parse(message.getMessage());
                Command comando = Interpreter.getCommand(task);
                if (comando.execute() == 0) {
                    responseMessage.setMessage("Comando ejecutado correctamente");
                } else {
                    responseMessage.setMessage("El comando no se pudo ejecutar");
                }
                threadControl.getOutQueue().put(responseMessage);
            } catch (ParseException e) {
                responseMessage.setMessage(e.getMessage());
            } catch (ClassNotFoundException e) {
                responseMessage.setMessage("Comando no valido");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } else {
            System.out.println("Mensaje desconocido de: " + threadControl.getThread().getName());
            System.exit(0);
        }

        

    }

}
